package com.xiaozou.util;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

import com.google.common.collect.Lists;
import com.xiaozou.config.Config;
import com.xiaozou.domain.AnswerRsp;
import com.xiaozou.domain.QuestionReq;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

@Slf4j
public class HttpUtil {

    public static AnswerRsp getAnswer(String host, QuestionReq questionReq) {
        try {
            String url = StrUtil.format("http://{}:8080/api/test-robot/qa", host);
            String payload = JSONUtil.toJsonStr(questionReq);
            HttpResponse response = HttpRequest.post(url)
                    .header("Content-Type", "application/json")
                    .body(payload)
                    .execute();

            if (response.isOk()) {
                Map<String, Object> jsonRes = JSONUtil.toBean(response.body(), Map.class);
                boolean success = (boolean) jsonRes.get("success");
                if (success) {
                    List<Map<String, Object>> dataList = (List<Map<String, Object>>) jsonRes.get("data");
                    Map<String, Object> dataFirst = dataList.get(0);
                    // 确保所有必需的参数都存在
                    String[] requiredKeys = {"rewrite", "audit", "classifyAllIn", "classify"};
                    for (String key : requiredKeys) {
                        if (!dataFirst.containsKey(key)) {
                            dataFirst.put(key, null); // 或者提供默认值
                        }
                    }
                    if (!dataFirst.containsKey("recallAskMethodNameList")) {
                        dataFirst.put("recallAskMethodNameList", Lists.newArrayList());
                    }
                    if (!dataFirst.containsKey("rerankAskMethodNameList")) {
                        dataFirst.put("rerankAskMethodNameList", Lists.newArrayList());
                    }
                    return JSONUtil.toBean(JSONUtil.toJsonStr(dataFirst), AnswerRsp.class);
                } else {
                    System.out.println("请求失败，状态码: " + response.getStatus());
                    return null;
                }
            } else {
                System.out.println("请求失败，状态码: " + response.getStatus());
                return null;
            }
        } catch (Exception e) {
            System.out.println("请求时出错: " + e.getMessage());
            return null;
        }
    }

    public static void clearSession(String host, String chatBotShopId) {
        try {
            String url = StrUtil.format("http://{}:8080/api/test-robot/clear/session/{}", host, chatBotShopId);
            HttpResponse response = HttpRequest.post(url)
                    .header("Content-Type", "application/json")
                    .execute();
            if (response.isOk()) {
                Map<String, Object> jsonRes = JSONUtil.toBean(response.body(), Map.class);
                boolean success = (boolean) jsonRes.get("success");
                if (!success) {
                    System.out.println("请求失败，状态码: " + response.getStatus());
                }else {
                    System.out.println("清除会话成功");
                }
            } else {
                System.out.println("请求失败，状态码: " + response.getStatus());
            }
        } catch (Exception e) {
            System.out.println("请求时出错: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        clearSession(Config.QWEN_HOST,"2322057877175304392");
        QuestionReq questionReq = new QuestionReq();
        questionReq.setQuestion("我现在只要一个解决方案");
        questionReq.setChatbotShopId("2322057877175304392");
        questionReq.setMockContext("买家：我之前是不是都说明了\n买家：说的明明白白\n客服：收到再试下哦亲亲\n客服：收到后您把电脑的电源插头拔下来过10分钟一会再插上重新试一下。\n买家：你们现在是没检测到这个问题，我现在是有这问题，但是你们检测不出来，拿回来我依然有这个问题\n买家：你们怎么办，是不是我又要邮寄回去，然后你们继续没检测出来\n买家：买个破耳机在这踢皮球吗\n买家：我每次出点电流声我都以为自己耳鸣了\n买家：拿下来就没声音了\n买家：我不这么确定我找你干啥，耽误自己好几天用\n买家：还付了运费，在这弄得乱七八糟的事儿\n客服：这边检测确实没问题的\n客服：可能是电脑进电的情况呢\n买家：电脑进电吗，你是专业客服吗\n买家：你的意思是电脑没电吗");
        AnswerRsp answer = getAnswer(Config.QWEN_HOST, questionReq);
        System.out.println(JSONUtil.toJsonStr(answer));

    }


}
